package com.tgr.shortLink.project.service.impl;

import cn.hutool.core.annotation.Link;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tgr.shortLink.project.dao.entity.LinkDO;
import com.tgr.shortLink.project.dao.mapper.RecycleBinMapper;
import com.tgr.shortLink.project.dto.req.RecycleBinPageReqDTO;
import com.tgr.shortLink.project.dto.req.RecycleBinRecoverReqDTO;
import com.tgr.shortLink.project.dto.req.RecycleBinRemoveReqDTO;
import com.tgr.shortLink.project.dto.req.RecycleBinSaveReqDTO;
import com.tgr.shortLink.project.dto.resp.LinkPageRespDTO;
import com.tgr.shortLink.project.dto.resp.RecycleBinPageRespDTO;
import com.tgr.shortLink.project.service.RecycleBinService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import static com.tgr.shortLink.project.common.constant.RedisKeyConstant.GOTO_IS_NULL_SHORT_LINK_KEY;
import static com.tgr.shortLink.project.common.constant.RedisKeyConstant.GOTO_SHORT_LINK_KEY;

/**
 * 描述：
 * 作者：小陶不慌张
 * 文件：RecycleBinServiceImpl
 * 日期：2024/1/23 12:10
 */
@Service
@RequiredArgsConstructor
public class RecycleBinServiceImpl extends ServiceImpl<RecycleBinMapper, LinkDO> implements RecycleBinService {
    private final StringRedisTemplate stringRedisTemplate;
    @Override
    public void saveBin(RecycleBinSaveReqDTO requestParam) {
        LambdaQueryWrapper<LinkDO> queryWrapper = Wrappers.lambdaQuery(LinkDO.class)
                .eq(LinkDO::getGid,requestParam.getGid())
                .eq(LinkDO::getFullShortUrl,requestParam.getFullShortUrl())
                .eq(LinkDO::getEnableStatus,0)
                .eq(LinkDO::getDelFlag,0);
        LinkDO linkDO = LinkDO.builder()
                .enableStatus(1)
                .build();
        baseMapper.update(linkDO,queryWrapper);
        stringRedisTemplate.delete(String.format(GOTO_SHORT_LINK_KEY,requestParam.getFullShortUrl()));
    }

    @Override
    public IPage<RecycleBinPageRespDTO> pageBin(RecycleBinPageReqDTO requestParam) {
        LambdaQueryWrapper<LinkDO> pageWrapper = Wrappers.lambdaQuery(LinkDO.class)
                .eq(LinkDO::getGid,requestParam.getGid())
                .eq(LinkDO::getEnableStatus,1)
                .eq(LinkDO::getDelFlag,0)
                .orderByDesc(LinkDO::getCreateTime);
        // 查询分页
        IPage<LinkDO> resultPage= baseMapper.selectPage(requestParam,pageWrapper);
        //转换类型
        return resultPage.convert(each->{
            RecycleBinPageRespDTO result= BeanUtil.toBean(each,RecycleBinPageRespDTO.class);
            //为返回的域名添加上http://协议前缀
            result.setDomain("http://"+result.getDomain());
            return result;
        });

    }

    @Override
    public void recoverBin(RecycleBinRecoverReqDTO requestParam) {
        LambdaQueryWrapper<LinkDO> queryWrapper = Wrappers.lambdaQuery(LinkDO.class)
                .eq(LinkDO::getGid,requestParam.getGid())
                .eq(LinkDO::getFullShortUrl,requestParam.getFullShortUrl())
                .eq(LinkDO::getEnableStatus,1)
                .eq(LinkDO::getDelFlag,0);
        LinkDO linkDO = LinkDO.builder()
                .enableStatus(0)
                .build();
        baseMapper.update(linkDO,queryWrapper);
        stringRedisTemplate.delete(String.format(GOTO_IS_NULL_SHORT_LINK_KEY,requestParam.getFullShortUrl()));
    }

    @Override
    public void removeBin(RecycleBinRemoveReqDTO requestParam) {
        LambdaQueryWrapper<LinkDO> queryWrapper = Wrappers.lambdaQuery(LinkDO.class)
                .eq(LinkDO::getGid,requestParam.getGid())
                .eq(LinkDO::getFullShortUrl,requestParam.getFullShortUrl())
                .eq(LinkDO::getEnableStatus,1)
                .eq(LinkDO::getDelFlag,0);
        //逻辑删除，在yaml中配置mybatisPlus
        baseMapper.delete(queryWrapper);

    }

}
